package panda.client.listeners;

import panda.client.Cardpanda;
import panda.client.JSNI;
import panda.client.ui.widgets.DraggableCard;
import panda.client.ui.widgets.InPlay;
import panda.client.ui.widgets.Tooltip;

import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Widget;

public class ToolTipHandler implements MouseMoveHandler, MouseOverHandler, MouseDownHandler, MouseUpHandler,
		MouseOutHandler {
	/* where we want the tooltip to appear in relation to the cursor */
	private static final int xTooltipOffset = 0;
	private static final int yTooltipOffset = 0;

	private final Tooltip tip = Tooltip.get();

	/* tooltip shows the card's name unless it is face down and you don't own it */
	private void showTooltip(String cardName) {
		tip.setVisible(true);
		tip.fadeInTooltip(400);
		
		tip.setText(cardName);
		
		DOM.setStyleAttribute(tip.getElement(), "opacity", "0.0");
		DOM.setStyleAttribute(tip.getElement(), "filter", "alpha(opacity=0)");
	}

	/**
	 * Moves the tooltip with the mouse cursor. Takes window scrolling coordinates into account.
	 */
	public void onMouseMove(MouseMoveEvent event) {
		int x = event.getX();
		int y = event.getY();
		Widget source = (Widget) event.getSource();

		int scrollY = JSNI.getScrollY();
		int scrollX = JSNI.getScrollX();
		int xTooltipPosition = x + source.getAbsoluteLeft() + scrollX + xTooltipOffset;
		int yTooltipPosition = y + source.getAbsoluteTop() + scrollY + yTooltipOffset;
		Cardpanda.canvas.setWidgetPosition(tip, xTooltipPosition, yTooltipPosition);
	}

	/**
	 * Unhides tooltip and fades it in. Needs to watch if the card is dragging because fast dragging causes lots of
	 * sequential onMouseEnter and onMouseLeave events.<br>
	 */
	public void onMouseOver(MouseOverEvent event) {
		/* InPlay can be both Counter or a DraggableCard. maybe more, see InPlay */
		InPlay obj = (InPlay) event.getSource();

		if(!obj.isDragging()) {
			displayTooltip(obj);
		}
	}

	public void onMouseDown(MouseDownEvent event) {
		Tooltip.get().fadeOutTooltip(400);
	}

	public void onMouseUp(MouseUpEvent event) {
		InPlay obj = (InPlay) event.getSource();
		
		displayTooltip(obj);
	}
	
	private void displayTooltip(InPlay obj) {
		if(obj instanceof DraggableCard) {
			DraggableCard card = (DraggableCard) obj;
			
			/* if the card is owned by this client, always show the real name in the tooltip, regardless if face up or down */
			if(card.isMine()) {
				showTooltip(card.getMagicCard().getNameAndServerID());
			} else {
				/* if it's not owned by this client, use card.getName() */
				/* card.getName() returns "a face down card" if card is face down */
				showTooltip(card.getName());
			}
		} else {
			showTooltip(obj.getName());
		}
	}

	public void onMouseOut(MouseOutEvent event) {
		tip.setVisible(false);
	}
}